package com.sxpi.user.config;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @Author: 董伟豪
 * @Date: 2022/11/11 22:01
 * 初始化zookeeper的client对象
 */
@Configuration
public class CuatorFrameworkConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public CuratorFramework curatorFramework(){
        // 创建一个CuratorFramework第项
        CuratorFramework curatorFramework = CuratorFrameworkFactory
                .builder().connectString(environment.getProperty("zk.host"))
                .namespace(environment.getProperty("zk.namespace"))
                .retryPolicy(new RetryNTimes(5,1000)).build();

        // 启动服务
        curatorFramework.start();
        // 返回服务
        return curatorFramework;
    }
}
